<?php 
class TusergroupController extends AppController {

	var $name = 'Tusergroup';
	var $components = array('Xacl');
	var $uses = Array('Tusergrouprelation', 'Tactiongrouprelation', 'Tusergroup', 'Taction', 'Tmenu', 'Tuser', 'Tmenugrouprelation');
	function index() {

		$this->set('grouplist', $this->Tusergroup->findAll());
		$this->layout = 'tuser';
	}
	/**
     * create new group
     *
     */
	function newgroup() {
		if (! empty($_POST)) {
			$data = array('f_name'=>isset($_POST["fname"]) ? trim($_POST["fname"]) : '', 'f_desc'=>isset($_POST["fdesc"]) ? trim($_POST["fdesc"]) : '', );

			if ( empty($data['f_name'])) {
				$this->flash("组名不能为空!", "/tusergroup");
				return;
			}
			if (0 < $this->Tusergroup->findCount("f_name='{$data['f_name']}'")) {
				$this->flash("组名被占用", "/tusergroup");
				return;
			}
			$this->Tusergroup->save($data);
			$this->flash("添加成功!", "/tusergroup");
			return;
		}
		$this->flash("组名不能为空!", "/tusergroup");
		return;
	}
	/**
     * delete user group
     *
     * @param int $id group id
     */
	function delgroup($id = NULL) {
		if (NULL == $id) {
			$this->flash("操作无效!", "/tusergroup");
			return;
		}
		if (0 < $this->Tusergrouprelation->findCount("F_GID='$id'")) {
			$this->flash("组内还有用户，请先清空用户数据!", "/tusergroup");
			return;
		}
		$this->Tusergroup->id = $id;
		$this->Tusergroup->delete();
		$this->flash("删除成功!", "/tusergroup");
		return;

	}
	/**
     * set acl for goup form
     *
     * @param int $id	group id
     * @return  
     */
	function configacl($id = NULL) {
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$groupActions = $this->Tactiongrouprelation->getGroupActions($id);
		$rs = $this->Taction->findAll(NULL, NULL, 'f_action desc');
		$data = array();
		if (count($rs > 0)) {
			foreach ($rs as $k=>$val) {
				$tmp = $val;
				$tmp['Taction']['is_checked'] = false;
				if (in_array($tmp['Taction']['id'], $groupActions)) {
					$tmp['Taction']['is_checked'] = true;
				}
				$data[] = $tmp;
			}
		}
		$this->set('actionlist', $data);
		$this->set('gid', $id);
		$this->layout = NULL;
	}
	/**
	 * set acl for goup action
	 *
	 * @return  
	 */
	function configacldo() {
		$id = isset($_POST['gid']) ? $_POST['gid'] : NULL;
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$new_data = array();
		$del_data = array();
		$groupActions = $this->Tactiongrouprelation->getGroupActions($id);
		$_aids = isset($_POST['a_id']) ? $_POST['a_id'] : array();
		if (0 == count($_aids)) {
			$this->Tactiongrouprelation->deleteAll("f_gid='$id'");
			$this->flash('操作成功!', '/tusergroup/configacl/'.$id);
			return '';
		}
		if (0 == count($groupActions)) {
			$new_data = $_aids;
		} else {
			foreach ($_aids as $val) {
				if (!in_array($val, $groupActions) && !in_array($val, $new_data)) {
					$new_data[] = $val;
				}
			}
			foreach ($groupActions as $val) {
				if (!in_array($val, $new_data) && !in_array($val, $_aids)) {
					$del_data[] = $val;
				}
			}

		}


		if (0 < count($del_data)) {
			foreach ($del_data as $value) {

				$d_id = $this->Tactiongrouprelation->field('id', "f_aid ='$value' and f_gid='$id'");
				$this->Tactiongrouprelation->delete($d_id);
				//echo $value;
			}

		}
		if (0 < count($new_data)) {
			// print_r($new_data);
			foreach ($new_data as $value) {
				$r = Array();
				$r = array('id'=>false, 'f_gid'=>trim($id), 'f_aid'=>trim($value));
				$this->Tactiongrouprelation->save($r);
				//unset($r);

			}

		}

		$this->flash('操作成功!', '/tusergroup/configacl/'.$id);
		return '';
	}

	/**
     * set menu for group form
     * @param int $id group id
     * @return
     */
	function configmenu($id = NULL) {
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$GroupMenus = $this->Tmenugrouprelation->getGroupMenus($id);
		$rs = $this->Tmenu->findAll(NULL, NULL, 'f_name desc');
		$data = array();
		if (count($rs) > 0) {
			foreach ($rs as $k=>$val) {
				$tmp = $val;
				$tmp['Tmenu']['is_checked'] = false;
				if (in_array($tmp['Tmenu']['id'], $GroupMenus)) {
					$tmp['Tmenu']['is_checked'] = true;
				}
				$data[] = $tmp;
			}
		}
		$this->set('menulist', $data);
		$this->set('gid', $id);
		$this->layout = NULL;
	}
	/**
	 * set menu for group action
	 *
	 * @return  
	 */
	function configmenudo() {
		$id = isset($_POST['gid']) ? $_POST['gid'] : NULL;
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$new_data = array();
		$del_data = array();
		$GroupMenus = $this->Tmenugrouprelation->getGroupMenus($id);
		$_aids = isset($_POST['m_id']) ? $_POST['m_id'] : array();
		if (0 == count($_aids)) {
			$this->Tmenugrouprelation->deleteAll("f_gid='$id'");

			$this->flash('操作成功!', '/tusergroup/configmenu/'.$id);
			return '';
		}
		if (0 == count($GroupMenus)) {
			$new_data = $_aids;
		} else {
			foreach ($_aids as $val) {
				if (!in_array($val, $GroupMenus) && !in_array($val, $new_data)) {
					$new_data[] = $val;
				}
			}
			foreach ($GroupMenus as $val) {
				if (!in_array($val, $new_data) && !in_array($val, $_aids)) {
					$del_data[] = $val;
				}
			}

		}

		// print_r($del_data);
		if (0 < count($del_data)) {
			foreach ($del_data as $value) {

				$d_id = $this->Tmenugrouprelation->field('id', "f_mid ='$value' and f_gid='$id'");
				$this->Tmenugrouprelation->delete($d_id);
				//echo $value;
			}

		}
		if (0 < count($new_data)) {

			foreach ($new_data as $value) {
				$r = Array();
				$r = array('id'=>false, 'f_gid'=>trim($id), 'f_mid'=>trim($value));

				$this->Tmenugrouprelation->save($r);
				//unset($r);

			}

		}

		$this->flash('操作成功!', '/tusergroup/configmenu/'.$id);
		return '';
	}


	/**
     * set users for group form
     * @param int $id group id
     * @return
     */
	function configusers($id = NULL) {
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$GroupUsers = $this->Tusergrouprelation->getGroupUsers($id);
		$rs = $this->Tuser->findAll(NULL, NULL, 'f_uname desc');
		$data = array();
		if (count($rs) > 0) {
			foreach ($rs as $k=>$val) {
				$tmp = $val;
				$tmp['Tuser']['is_checked'] = false;
				if (in_array($tmp['Tuser']['id'], $GroupUsers)) {
					$tmp['Tuser']['is_checked'] = true;
				}
				$data[] = $tmp;
			}
		}
		$this->set('userlist', $data);
		$this->set('gid', $id);
		$this->layout = NULL;
	}
	/**
	 * set users for group action
	 *
	 * @return  
	 */
	function configusersdo() {
		$id = isset($_POST['gid']) ? $_POST['gid'] : NULL;
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$new_data = array();
		$del_data = array();
		$GroupUsers = $this->Tusergrouprelation->getGroupUsers($id);
		$_aids = isset($_POST['u_id']) ? $_POST['u_id'] : array();
		if (0 == count($_aids)) {
			$this->Tusergrouprelation->deleteAll("f_gid='$id'");

			$this->flash('操作成功!', '/tusergroup/configusers/'.$id);
			return '';
		}
		if (0 == count($GroupUsers)) {
			$new_data = $_aids;
		} else {
			foreach ($_aids as $val) {
				if (!in_array($val, $GroupUsers) && !in_array($val, $new_data)) {
					$new_data[] = $val;
				}
			}
			foreach ($GroupUsers as $val) {
				if (!in_array($val, $new_data) && !in_array($val, $_aids)) {
					$del_data[] = $val;
				}
			}

		}

		// print_r($del_data);
		if (0 < count($del_data)) {
			foreach ($del_data as $value) {

				$d_id = $this->Tusergrouprelation->field('id', "f_uid ='$value' and f_gid='$id'");
				$this->Tusergrouprelation->delete($d_id);
				//echo $value;
			}

		}
		if (0 < count($new_data)) {

			foreach ($new_data as $value) {
				$r = Array();
				$r = array('id'=>false, 'f_gid'=>trim($id), 'f_uid'=>trim($value));

				$this->Tusergrouprelation->save($r);
				//unset($r);

			}

		}

		$this->flash('操作成功!', '/tusergroup/configusers/'.$id);
		return '';
	}
}
?>
